﻿// DateTimeToStringConverter
//
// WPF data binding conversion class for converting DateTime to String
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.Text;
using System.Windows.Data;

namespace FLMScan.WPF
{
    [ValueConversion(typeof(object), typeof(string))]
    public class DateTimeToStringConverter : BaseConverter, IValueConverter
    {
        // convert from DateTime to string
        public object Convert(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
        {
            DateTime dateTime = (DateTime)value;
            return dateTime.ToString();
        }

        // convert from string to DateTime
        public object ConvertBack(object value, Type targetType, object parameter,
                        System.Globalization.CultureInfo culture)
        {
            String s = (String)value;
            int minutes = 0;
            int seconds = 0;
            char[] separators = new char[]{':'};
            String[] tokens = s.Split(separators);
            if( tokens.Length>2)
            {
                minutes = int.Parse(tokens[0]);
                seconds = int.Parse(tokens[1]);
            }

            DateTime dateTime = new DateTime(0);
            
            //dateTime.Minute = minutes;
            //dateTime.Second = seconds;

            return dateTime;
        }
    }
}
